package com.tanzeer.deliverytracker.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tanzeer.deliverytracker.R;
import com.tanzeer.deliverytracker.models.RowItem;

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

	Context context;

	public CustomListViewAdapter(Context context, int resourceId,
			List<RowItem> items) {
		super(context, resourceId, items);
		this.context = context;
	}

	/* private view holder class */
	private class ViewHolder {
		TextView orgin;
		TextView destination;
		TextView txtTrack;
		TextView txtDesc;
		TextView txtNameDesc;
		ImageView categoryImageView;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		RowItem rowItem = getItem(position);

		LayoutInflater mInflater = (LayoutInflater) context
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_item_actives, null);
			holder = new ViewHolder();
			holder.txtNameDesc = (TextView) convertView.findViewById(R.id.trackdesc);
			holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
			holder.orgin = (TextView) convertView.findViewById(R.id.origin);
			holder.destination = (TextView) convertView.findViewById(R.id.destination);
			holder.txtTrack = (TextView) convertView.findViewById(R.id.track);
			holder.categoryImageView = (ImageView)convertView.findViewById(R.id.imageView1);
			convertView.setTag(holder);
		} else
			holder = (ViewHolder) convertView.getTag();
		if(rowItem.itemSummary.origin != null) {
		   holder.orgin.setText("O: "+rowItem.itemSummary.origin);
		} else {
			holder.orgin.setVisibility(View.GONE);	
		}
		
	//	if (rowItem.itemSummary.destination != null) {

			holder.txtNameDesc.setText("Track Name: "
					+ rowItem.itemSummary.trackName);
	//	}
		
		if (rowItem.itemSummary.destination != null) {
			holder.destination.setText("D: "+rowItem.itemSummary.destination);
		} else {
			holder.destination.setVisibility(View.GONE);
		}

		holder.txtTrack.setText("Track No: "+rowItem.itemSummary.trackno);
		
		if (rowItem.itemSummary.status != null) {
			if (rowItem.itemSummary.status.equalsIgnoreCase("NOT DELIVERED")) {
				holder.txtDesc.setTextColor(Color.RED);
				holder.txtDesc.setText("Status: " + "NOT DELIVERED");
			} else if (rowItem.itemSummary.status.equalsIgnoreCase("DELIVERED")) {
				holder.txtDesc.setTextColor(Color.GREEN);
				holder.txtDesc.setText("Status: " + "DELIVERED");
			}
		}
		
		if (rowItem.itemSummary.category != null) {
			if ("Incoming".equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.incoming));
			} else if ("Outgoing"
					.equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.outgoing));
			} else if ("Favorite"
					.equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.favorite));
			} else if ("Movie".equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.film_reel));
			} else if ("Gift".equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.present));
			} else if ("Heart".equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.heart));
			} else if ("Business"
					.equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.business));
			} else if ("Important"
					.equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.important));
			} else if ("Camera".equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView
						.setImageDrawable(context.getResources().getDrawable(
								R.drawable.camera_unmount2));
			} else if ("Clothes".equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.clothes));
			} else if ("Computer"
					.equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.mycomputer));
			} else if ("Games".equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.gaming_pad));
			} else if ("Food Dishes"
					.equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.fooddishes));
			} else if ("Light".equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.light));
			} else if ("Music".equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.kbemusedsrv));
			} else if ("Office Materials"
					.equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.office));
			} else if ("Paint".equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.kolourpaint));
			} else if ("Medicine"
					.equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.pill));
			} else if ("Flowers".equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.flowers));
			} else if ("Sports".equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.sports));
			} else if ("Tools".equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.tool2));
			} else if ("None".equalsIgnoreCase(rowItem.itemSummary.category)) {
				holder.categoryImageView.setImageDrawable(context
						.getResources().getDrawable(R.drawable.white));
			}
		}
		
		return convertView;
	}

	private void configrowItem(RowItem rowItem) {
		// TODO Auto-generated method stub
		
	}
}
